package com.api.rocky.base;


import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.api.rocky.R;
import com.api.rocky.Util.ImageLoader;
import com.api.rocky.model.UserList;

    public class MyAdapter extends BaseAdapter{ 
    	private static final String TAG = "MyAdapter";
    	private boolean mBusy = false;
    	private ImageLoader mImageLoader;

    	public void setFlagBusy(boolean busy) {
    		this.mBusy = busy;
    	}
  
        private LayoutInflater mInflater;
        private List<Map<String, Object>> mData;
          
          
        public MyAdapter(Context context,List<Map<String, Object>> mData){ 
            this.mInflater = LayoutInflater.from(context);
            this.mData = mData;
            mImageLoader = new ImageLoader();
        } 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return mData.size(); 
        } 
  
        public Object getItem(int arg0) { 
            // TODO Auto-generated method stub 
            return null; 
        } 
  
        public long getItemId(int arg0) { 
            // TODO Auto-generated method stub 
            return 0; 
        } 
  
        public View getView(int position, View convertView, ViewGroup parent) { 
              
            UserList holder = null; 
            if (convertView == null) { 
                  
                holder=new UserList();   
                  
                convertView = mInflater.inflate(R.layout.listview_user, null); 
                holder.face = (ImageView)convertView.findViewById(R.id.img); 
                holder.name = (TextView)convertView.findViewById(R.id.name); 
                holder.age = (TextView)convertView.findViewById(R.id.age); 
                holder.sex = (ImageView)convertView.findViewById(R.id.sex); 
                holder.distance = (TextView)convertView.findViewById(R.id.distance);
                holder.time = (TextView)convertView.findViewById(R.id.time);
                holder.location = (TextView)convertView.findViewById(R.id.location);
                convertView.setTag(holder); 
                  
            }else { 
                  
                holder = (UserList)convertView.getTag(); 
            } 
            if (!mBusy) {
    			mImageLoader.loadImage((String)mData.get(position).get("face"), this, holder);
    		} else {
    			Bitmap bitmap = mImageLoader.getBitmapFromCache((String)mData.get(position).get("face"));
    			if (bitmap != null) {
    				holder.face.setImageBitmap(bitmap);
    			} else {
    				holder.face.setImageResource(R.drawable.ic_launcher);
    			}
    		}  
              
            holder.name.setText((String)mData.get(position).get("name")); 
            holder.age.setText((String)mData.get(position).get("age")); 
            holder.sex.setBackgroundResource((Integer)mData.get(position).get("sex")); 
            holder.distance.setText((String)mData.get(position).get("distance")); 
            holder.time.setText((String)mData.get(position).get("time")); 
            holder.location.setText((String)mData.get(position).get("location")); 
            return convertView; 
        } 
          
    } 
